package com.tanhua.autoconfig.templates;

import com.baidu.aip.face.AipFace;
import com.tanhua.autoconfig.properties.AipFaceProperties;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;

public class AipFaceTemplate {

    @Autowired
    private AipFaceProperties properties;

    /**
     * 测试是否包含人脸
     *   参数：图片url
     *   返回值：
     *     true：包含，false：不包含
     * @return
     */
    public Boolean detect(String image) {
//        String image = "https://171tanhua.oss-cn-beijing.aliyuncs.com/2022/01/20/e4b88c1a-55c1-40cb-9195-259127c66691.png";

        AipFace client = new AipFace(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
            options.put("face_field", "age");
            options.put("max_face_num", "2");
            options.put("face_type", "LIVE");
            options.put("liveness_control", "LOW");

        //图片（url，图标base64编码，图片token字符串）

        String imageType = "URL";

        // 人脸检测
        JSONObject res = client.detect(image, imageType, options);
        //返回值map对象。error_code = 0（包含人脸）
        System.out.println(res.toString(2));

        int error_code = res.getInt("error_code");
        return error_code == 0;
    }
}
